package lv.solcraft.replication.server.rest.restlets;

import java.util.ArrayList;
import java.util.Collections;

import lv.solcraft.replication.server.boot.Booter;
import net.sf.json.JSONArray;

import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.data.CharacterSet;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.StringRepresentation;

public class ListServersRestlet extends Restlet {
    private final Booter booter;

    public ListServersRestlet(Context context, Booter booter) {
        super(context);
        this.booter = booter;
    }

    @Override
    public void handle(Request request, Response response) {
        ArrayList<String> servers = new ArrayList<String>(booter.getServers());
        Collections.sort(servers);
        JSONArray json = JSONArray.fromObject(servers);
        response.setEntity(new StringRepresentation(json.toString(), MediaType.TEXT_PLAIN, Language.ALL, CharacterSet.UTF_8));
    }
}
